<template>
    <div class="box">
        <header class="header">
            <NavBar title="登录" />
        </header>
        <main class="content">
            <van-form @submit="onSubmit">
                <van-cell-group inset>
                    <van-field v-model="username" name="username" label="用户名" placeholder="用户名" :rules="[
                        { required: true, message: '请填写用户名' },
                        {
                            pattern: /^1[3-9]\d{9}$/,
                            message: '请输入正确的手机号'
                        }]" />
                    <van-field v-model="password" type="password" name="password" label="密码" placeholder="密码"
                        :rules="[{ required: true, message: '请填写密码' }]" />
                    <van-field v-model="sms" center clearable label="短信验证码" placeholder="请输入短信验证码" :rules="[
                        { pattern: /^\d{6}$/, message: '请输入6位数字' }]">
                        <template #button>
                            <van-button size="small" type="primary" @click="sendcode"
                                :disabled="msg != '获取验证码' || username.length != 11">
                                {{ msg }}
                            </van-button>
                        </template>
                    </van-field>
                </van-cell-group>
                <div style="margin: 16px;">
                    <van-button round block type="primary" native-type="submit"
                        :disabled="username.length == 0 || sms.length != 6">
                        登录
                    </van-button>
                </div>
            </van-form>
            <div style="display:flex;justify-content: space-between;color:#646566;padding: 0 25px;">
                <p>短信登录</p>
                <p @click="$router.push('/register')">快速注册</p>
            </div>
        </main>
    </div>
</template>
<script setup>
import NavBar from '@/components/NavBar.vue';
import { ref } from 'vue';
import request from '@/utils/request'
import { showSuccessToast, showFailToast } from 'vant';
import { useRouter } from 'vue-router';
import md5 from 'blueimp-md5'
import { useCounterStore } from '@/stores/counter'
// 可以在组件中的任意位置访问 `store` 变量 ✨
const store = useCounterStore()
const username = ref('');
const password = ref('');
const router = useRouter()
const msg = ref('获取验证码')
const sms = ref('')
const timer = ref(null)
const count = ref(10)
const onSubmit = (values) => {
    console.log('submit', values);
    request.post('/login', {
        username: values.username,
        password: md5(values.password),
        sms: sms.value
    }).then(res => {
        if (res.data.code == 200) {
            // 提示登录成功
            showSuccessToast(res.data.message);
            // // 存储token
            localStorage.setItem('token', res.data.token)
            // 存储用户信息
            localStorage.setItem('username', values.username)
            // 跳转页面
            router.push('/')
        } else {
            // 提示登录失败
            showFailToast(res.data.message);
        }
    })
};
const sendcode = () => {
    msg.value = count.value + 's后重新发送'
    request.get('/code').then(res => {
        sms.value = res.data.data
    })
    timer.value = setInterval(() => {
        count.value--
        if (count.value == 0) {
            // 清除定时器
            clearInterval(timer.value)
            // 重置button按钮的文字
            msg.value = '获取验证码'
            // 重置count的值
            count.value = 10
        } else {
            msg.value = count.value + 's后重新发送'
        }
    }, 1000)
}
</script>